# 根据LS-DYNA手册，*NODE关键字在固定格式下，
# 各字段的列位置通常是：节点ID（1-8列），
# X坐标（9-24列），Y坐标（25-40列），
# Z坐标（41-56列），TC（57-64列），
# RC（65-72列）

class K_FILE_KEY_WORD:
    NODE = "*NODE"
    ELEMENT_SHELL = "*ELEMENT_SHELL"
    END = "*END"


class LS_DYNA_Reader:
    node_read_start = 0
    element_shell_read_start = 0
    nodes = None
    node_ids = None
    nodes_tc = None
    nodes_rc = None
    elements = None
    keys = K_FILE_KEY_WORD()

    def __init__(self):
        self.nodes = []
        self.node_ids = []
        self.nodes_tc = []
        self.nodes_rc = []
        self.elements = []

    def read(self, file_path: str):
        with open(file_path, 'r', encoding='utf-8') as file:
            for line in file:
                if self.node_read_start == 1:
                    if line.find('*') != -1:
                        self.node_read_start = 0
                    else:
                        self.parse_node_line(line)
                    continue
                # if self.element_shell_read_start == 1:
                #     if line.find('*') != -1:
                #         self.element_shell_read_start = 0
                #     else:
                #         self.parse_element_shell_line(line)
                #     continue
                if line.find(self.keys.NODE) != -1:
                    self.node_read_start = 1
                    continue
                if line.find(self.keys.ELEMENT_SHELL) != -1:
                    continue
        print(self.elements)

    def parse_node_line(self, line):
        node_id = int(line[0:8])
        x = float(line[8:24])
        y = float(line[24:40])
        z = float(line[40:56])
        TC = float(line[56:64])
        RC = float(line[64:72])
        self.node_ids.append(node_id)
        self.nodes.append(x)
        self.nodes.append(y)
        self.nodes.append(z)
        self.nodes_tc.append(TC)
        self.nodes_rc.append(RC)

    def parse_element_shell_line(self, line):
        node_id = int(line[0:8])
        pid = int(line[8:16])
        n1 = int(line[16:24])
        n2 = int(line[24:32])
        n3 = int(line[32:40])
        n4 = int(line[40:48])
        self.elements.append({
            "eid": node_id,
            "pid": pid,
            "node_ids": [n1, n2, n3, n4],
            "type": "shell"
        })


LS_DYNA_Reader().read("C:/Users/Chris/Downloads/stp_demo/result.key")
